import sensor from '@system.sensor';
export default {
    data: {
        min:0,
        xValue:0,
//        maxValue:0,
/*        yValue:0,
        zValue:0,*/
        score:0,
        timer:null,
        information:'早晚刷牙，饭后漱口，牙齿健康!'
    },
    computed:{
        showXValue(){
            return parseInt(Math.abs(this.xValue));
        }

    },
/*    onShow(){
        if(this.showXValue>this.maxValue){
            this.maxValue=this.showXValue;
        }
    },*/
    startBtn(){
        console.info("---------------开始");
//        考虑到setInterval内的this指向问题
        var that=this;
       this.timer= setInterval(function(){
           that.min++;
            console.info(that.min);
        },10000);
        var that=this;
        sensor.subscribeAccelerometer({
            interval: 'normal',
            success: function(ret) {

                that.xValue=ret.x;
/*                that.yValue=ret.y;
                that.zValue=ret.z;*/
            },
            fail: function(data, code) {
                console.info('Subscription failed. Code: ' + code + '; Data: ' + data);
            },
        });

/*        当手机水平放在桌面上的时候，
        x水平朝右为正
        y水平向前为正
        z垂直向上为正*/
    },
    stopBtn(){
        console.info("---------------停止");
        clearInterval(this.timer);

        let grade=100;
        let inf='';

        if(this.showXValue<2){
            grade-=15;
            inf+='刷牙力度太小，';
        }
        else if(this.showXValue>=2&&this.showXValue<=10){
            inf+='刷牙力度很好，';
        }
        else {
            grade-=25;
            inf+='刷牙力度太大，';
        }

        if(this.min<1){
            grade-=15;
            inf+='刷牙时间过短';
        }
        else if(this.min>=1&&this.min<=3){
            inf+='刷牙时间很好';
        }
        else {
            grade-=25;
            inf+='刷牙时间过长';
        }
        inf+='!';
        this.score=grade;
        this.information=inf;


/*        if(this.showXValue<this.maxValue){
            this.showXValue=this.maxValue;
        }*/
        console.info("max"+this.maxValue);
        sensor.unsubscribeAccelerometer();
    },
/*    resetBtn(){
        console.info("---------------复位");
        this.min=0;
        this.score=0;
        this.information='早晚刷牙，饭后漱口，牙齿健康!';
        clearInterval(this.timer);
    },*/
    onHide(){
        sensor.unsubscribeAccelerometer();
    },
    saveBtn:async function(){

        //要传递的数据
//        var data1 = this.score;
        var data1 ={
            lidu:this.showXValue,
            min:this.min,
            score:this.score,
            information:this.information
    }
        //action对象，相当于intent
        var action = {};

        //添加service所在的包名
        action.bundleName = 'com.example.smartmirror2';

        //添加service的类名
        action.abilityName = 'com.example.smartmirror2.SyncServiceAbility';

        //添加messageCode ，用于区分其他的action
        action.messageCode = 110;

        //添加数据
        action.data = data1;

        //跳转的activity的类型，0代表service
        action.abilityType = 0;

        //最重要的一步：跳转
        var str = await FeatureAbility.callAbility(action);

        //下面是处理 java FA 返回的数据
        var result = JSON.parse(str);
//        console.info(JSON.stringify(result));
/*        console.info(result.lidu);
        console.info(result.min);
        console.info(result.score);*/
/*        this.showXValue=parseInt(result.lidu);
        this.min=parseInt(result.min);
        this.score=parseInt(result.score);*/

        console.info('同步数据');
    },
    getBtn:async function(){
        var data2
        //action对象，相当于intent
        var action = {};

        //添加service所在的包名
        action.bundleName = 'com.example.smartmirror2';

        //添加service的类名
        action.abilityName = 'com.example.smartmirror2.SyncServiceAbility';

        //添加messageCode ，用于区分其他的action
        action.messageCode = 111;

        //添加数据
        action.data = data2;

        //跳转的activity的类型，0代表service
        action.abilityType = 0;

        //最重要的一步：跳转
        var str = await FeatureAbility.callAbility(action);

        //下面是处理 java FA 返回的数据
        var result = JSON.parse(str);
/*        console.info(result.lidu);
        console.info(result.min);
        console.info(result.score);
        this.showXValue=parseInt(result.lidu);
        this.min=parseInt(result.min);
        this.score=parseInt(result.score);*/
        console.info("这是手机b")
        console.info(result.showXValue);
        console.info(result.min);
        console.info(result.score);
        console.info(result.information);
        this.showXValue=parseInt(result.lidu);
        this.min=parseInt(result.min);
        this.score=parseInt(result.score);
        this.information=result.information;

    }
}